//
//  SettingViewController.m
//  SDKUI
//
//  Created by aiquantong on 17/08/2017.
//  Copyright © 2017 aiquantong. All rights reserved.
//

#import "ProfileViewController.h"

#import "CCKitManger.h"
#import "ProfileManager.h"

#import "PhotoUtil.h"
#import "HUDUtils.h"
#import "CacheUtil.h"


@interface ProfileViewController ()

@property (nonatomic, strong) IBOutlet UIImageView *headerImageView;

@property (nonatomic, strong) IBOutlet UITextField *phoneTextField;
@property (nonatomic, strong) IBOutlet UIView *phoneView;


@end

@implementation ProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.phoneView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    [HUDUtils showHUDOnWindowWithText:@"获取信息"];
    
    __weak typeof(self) weekSelf = self;
    if ([CCKitManger shareInstance].customProfile &&
        [[CCKitManger shareInstance].customProfile respondsToSelector:@selector(customProfileRetireWithUserId:callback:)]) {
        [[CCKitManger shareInstance].customProfile
         customProfileRetireWithUserId:[CCKitManger getcurLoginUser]
         callback:^(CustomProfileModel *model) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 [HUDUtils hideHUDForWindow];
                 if (model!= nil && weekSelf) {
                     typeof(self) strongSelf = weekSelf;
                     if (model.nickName) {
                         strongSelf.phoneTextField.text = model.nickName;
                     }
                     
                     if (model.headerImg) {
                         [strongSelf.headerImageView setImage:model.headerImg];
                     }
                 }
             });
         }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)onclickPhoto:(id)sender
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *photoAction = [UIAlertAction
                                  actionWithTitle:@"相册"
                                  style:UIAlertActionStyleDefault
                                  handler:^(UIAlertAction *action) {
                                      [PhotoUtil presentEditPhotoLibraryWithPersentViewController:self.navigationController
                                                                                     delegate:self
                                                                                          tag:0];
                                  }];
    [alertController addAction:photoAction];
    
    UIAlertAction *cameraAction = [UIAlertAction
                                   actionWithTitle:@"拍照"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction *action) {
                                       [PhotoUtil presentCameraWithPersentViewController:self.navigationController
                                                                                delegate:self
                                                                                     tag:1];
                                   }];
    [alertController addAction:cameraAction];
    
    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:@"取消"
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action) {
                                       
                                   }];
    [alertController addAction:cancelAction];
    
    [self.navigationController presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    if ([mediaType isEqualToString:@"public.image"]) {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        NSData *imageDate = UIImageJPEGRepresentation(image, 0.5);
        [self.headerImageView setImage:image];
        NSString *imagePath = [CacheUtil getAbsolutePath:[CacheUtil saveSendImageFileWithData:imageDate chatName:[CCKitManger getcurLoginUser]]];
        [ProfileManager updateUserProfileImage:imagePath callback:^(NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error) {
                    [HUDUtils showHUDTxtOnWindowWithText:@"保存头像失败" hiddenAfter:2];
                }else{
                    [HUDUtils showHUDTxtOnWindowWithText:@"保存头像成功" hiddenAfter:2];
                }
            });
        }];
        
    }else if([mediaType isEqualToString:@"public.movie"]){
        NSURL *videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
        NSData *data = [NSData dataWithContentsOfURL:videoUrl];
        
    }else{
        
    }
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}



-(IBAction)onclickSave:(id)sender
{
    NSString *nickName = self.phoneTextField.text;
    [ProfileManager updateUserProfileName:nickName callback:^(NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error) {
                [HUDUtils showHUDTxtOnWindowWithText:@"保存昵称失败" hiddenAfter:2];
            }else{
                [HUDUtils showHUDTxtOnWindowWithText:@"保存昵称成功" hiddenAfter:2];
            }
        });
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
